package com.cch.demo.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cch.demo.bus.domain.Provider;
import com.cch.demo.bus.service.ProviderService;
import com.cch.demo.bus.vo.ProviderVo;
import com.cch.demo.sys.domain.Permission;
import com.cch.demo.sys.util.Constast;
import com.cch.demo.sys.util.DataGridView;
import com.cch.demo.sys.util.ResultObj;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 华哥
 * @since 2020-03-11
 */
@RestController
@RequestMapping("/provider")
public class ProviderController {

    @Resource
    private ProviderService providerServiceImpl;

    @RequestMapping("/loadAllProvider")
    public DataGridView loadAllProvider(ProviderVo providerVo){
        QueryWrapper<Provider> queryWrapper = new QueryWrapper<>();

        queryWrapper.like(StringUtils.isNotBlank(providerVo.getProvidername()),"providername",providerVo.getProvidername());
        queryWrapper.like(StringUtils.isNotBlank(providerVo.getTelephone()),"telephone",providerVo.getTelephone());
        queryWrapper.like(StringUtils.isNotBlank(providerVo.getConnectionperson()),"connectionperson",providerVo.getConnectionperson());
        IPage<Provider> page = new Page<>(providerVo.getPage(),providerVo.getLimit());
        providerServiceImpl.page(page,queryWrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    @RequestMapping("addProvider")
    public ResultObj addProvider(ProviderVo providerVo){
        try{
            providerServiceImpl.save(providerVo);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    @RequestMapping("updateProvider")
    public ResultObj updateProvider(ProviderVo providerVo){
        try{

            providerServiceImpl.updateById(providerVo);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    @RequestMapping("deleteProvider")
    public ResultObj deleteProvider(Integer id){
        try{
            providerServiceImpl.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    @RequestMapping("batchDeleteProvider")
    public ResultObj BatchDeleteProvider(ProviderVo providerVo){
        try{
            List<Serializable> idlist = new ArrayList<>();
            for (Integer id:providerVo.getIds()){
                idlist.add(id);
            }
            providerServiceImpl.removeByIds(idlist);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    @RequestMapping("loadAllProviderForSelect")
    public DataGridView loadAllProviderForSelect(){
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        //查询可用的供应商
        queryWrapper.eq("available", Constast.AVAILABLE_TRUE);
        List<Provider> list = providerServiceImpl.list();
        return new DataGridView(list);
    }
}

